package qlsl.androiddesign.view.subview.activityview;

import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.content.Intent;
import android.view.View;
import android.widget.BaseAdapter;
import qlsl.androiddesign.activity.subactivity.PostingCreateActivity;
import qlsl.androiddesign.activity.subactivity.PostingDetailActivity;
import qlsl.androiddesign.activity.subactivity.PostingHotActivity;
import qlsl.androiddesign.adapter.subadapter.PostingCategoryAdapter;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.Posting;
import qlsl.androiddesign.http.service.subservice.ForumService;
import qlsl.androiddesign.view.subview.activityview.peoplecenterbase.PeopleCenterBaseView;

/**
 * 热帖<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class PostingHotView extends PeopleCenterBaseView<Posting, PostingHotActivity> {

	public PostingHotView(PostingHotActivity activity) {
		super(activity);
	}

	protected void initView(View view) {
		super.initView(view);
		setTitle("热帖");
		setRightButtonResource(R.drawable.forum_posth);
		showRightButton();
	}

	protected void initData() {
		super.initData();
		query();
	}

	protected void initListener() {
		super.initListener();
	}

	@SuppressWarnings("unchecked")
	public <A> void showData(A... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			List<Posting> list_result = (List<Posting>) map.get("list");
			pager = (Pager) map.get("pager");
			list.addAll(list_result);
			notifyDataSetChanged();
		}
	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_right:
			doClickRightButton();
			break;
		case R.id.list_item:
			doClickListItem(view);
			break;
		}
	}

	private void query() {
		ForumService.queryPostingList(1, 1, this, activity);
	}

	private void doClickRightButton() {
		startActivityForResult(PostingCreateActivity.class, 0);
	}

	private void doClickListItem(View view) {
		int position = refreshView.getRefreshableView().getPositionForView(view);
		Posting item = (Posting) refreshView.getAdapter().getItem(position);
		Intent intent = new Intent(activity, PostingDetailActivity.class);
		intent.putExtra("data", item);
		startActivityForResult(intent, 0);
	}

	public BaseAdapter getAdapter() {
		return new PostingCategoryAdapter(activity, list);
	}

	public void onActivityResult(int arg0, int arg1, Intent intent) {
		if (hasRefresh(intent)) {
			setRefresh(true);
			query();
		}
	}

}
